/*
 * Copyright (C) 2010 TTPASI, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ttpasi.jgenere.initial_port.languagedata;

import java.util.Map;

import ttpasi.jgenere.initial_port.VigCrack;

/**
 * Java port of language data from PyGenere v 0.3.  See pygenere.py to learn 
 * more about the original PyGenere implementation, including author contact 
 * information and licensing and use terms.
 * <p/>
 * Base class for data objects with language alphabet character use statistics. 
 * Used by {@link VigCrack}.
 * <p/>
 * Note: This implementation only supports characters in the English alphabet, 
 * though multiple languages are supported.
 * <p/>
 * Note: While the cryptography implementations in jgenere.initial_port work 
 * correctly according to the provided test cases, they were not optimized or 
 * otherwise designed to make use of Java language and platform features.  They 
 * are simple ports of the original Python implementation of PyGenere, provided 
 * for academic purposes.  Visit the project web site at 
 * <a target="_blank" href="http://code.google.com/p/jgenere/">http://code.google.com/p/jgenere/</a> 
 * for preferred Java-based implementations of these cryptography algorithms.
 * <p/>
 * The original PyGenere v 0.3 language data code:
 * <pre>
 *   """
 *   Character frequencies taken from:
 *   http://www.csm.astate.edu/~rossa/datasec/frequency.html (English)
 *   http://www.characterfrequency.com/ (French, Italian, Portuguese, Spanish)
 *   http://www.santacruzpl.org/readyref/files/g-l/ltfrqger.shtml (German)
 *   """
 * 
 *   # The following are language-specific data on character frequencies.
 *   # Kappa is the "index of coincidence" described in the cryptography paper
 *   # (link above).
 *   
 *   __english_data = {
 *                     'A':8.167, 'B':1.492, 'C':2.782, 'D':4.253, 'E':12.702,
 *                     'F':2.228, 'G':2.015, 'H':6.094, 'I':6.996, 'J':0.153,
 *                     'K':0.772, 'L':4.025, 'M':2.406, 'N':6.749, 'O':7.507,
 *                     'P':1.929, 'Q':0.095, 'R':5.987, 'S':6.327, 'T':9.056,
 *                     'U':2.758, 'V':0.978, 'W':2.360, 'X':0.150, 'Y':1.974,
 *                     'Z':0.074, 'max_val':12.702, 'kappa':0.0667
 *                    }
 *
 *   __french_data = {
 *                    'A':8.11, 'B':0.903, 'C':3.49, 'D':4.27, 'E':17.22,
 *                    'F':1.14, 'G':1.09, 'H':0.769, 'I':7.44, 'J':0.339,
 *                    'K':0.097, 'L':5.53, 'M':2.89, 'N':7.46, 'O':5.38,
 *                    'P':3.02, 'Q':0.999, 'R':7.05, 'S':8.04, 'T':6.99,
 *                    'U':5.65, 'V':1.30, 'W':0.039, 'X':0.435, 'Y':0.271,
 *                    'Z':0.098, 'max_val':17.22, 'kappa':0.0746
 *                   }
 * 
 *   __german_data = {
 *                    'A':6.506, 'B':2.566, 'C':2.837, 'D':5.414, 'E':16.693,
 *                    'F':2.044, 'G':3.647, 'H':4.064, 'I':7.812, 'J':0.191,
 *                    'K':1.879, 'L':2.825, 'M':3.005, 'N':9.905, 'O':2.285,
 *                    'P':0.944, 'Q':0.055, 'R':6.539, 'S':6.765, 'T':6.742,
 *                    'U':3.703, 'V':1.069, 'W':1.396, 'X':0.022, 'Y':0.032,
 *                    'Z':1.002, 'max_val':16.693, 'kappa':0.0767
 *                   }
 * 
 *   __italian_data = {
 *                     'A':11.30, 'B':0.975, 'C':4.35, 'D':3.80, 'E':11.24,
 *                     'F':1.09, 'G':1.73, 'H':1.02, 'I':11.57, 'J':0.035,
 *                     'K':0.078, 'L':6.40, 'M':2.66, 'N':7.29, 'O':9.11,
 *                     'P':2.89, 'Q':0.391, 'R':6.68, 'S':5.11, 'T':6.76,
 *                     'U':3.18, 'V':1.52, 'W':0.00, 'X':0.024, 'Y':0.048,
 *                     'Z':0.958, 'max_val':11.57, 'kappa':0.0733
 *                    }
 * 
 *   __portuguese_data = {
 *                        'A':13.89, 'B':0.980, 'C':4.18, 'D':5.24, 'E':12.72,
 *                        'F':1.01, 'G':1.17, 'H':0.905, 'I':6.70, 'J':0.317,
 *                        'K':0.0174, 'L':2.76, 'M':4.54, 'N':5.37, 'O':10.90,
 *                        'P':2.74, 'Q':1.06, 'R':6.67, 'S':7.90, 'T':4.63,
 *                        'U':4.05, 'V':1.55, 'W':0.0104, 'X':0.272, 'Y':0.0165,
 *                        'Z':0.400, 'max_val':13.89, 'kappa':0.0745
 *                       }
 * 
 *   __spanish_data = {
 *                     'A':12.09, 'B':1.21, 'C':4.20, 'D':4.65, 'E':13.89,
 *                     'F':0.642, 'G':1.11, 'H':1.13, 'I':6.38, 'J':0.461,
 *                     'K':0.038, 'L':5.19, 'M':2.86, 'N':7.23, 'O':9.58,
 *                     'P':2.74, 'Q':1.37, 'R':6.14, 'S':7.43, 'T':4.49,
 *                     'U':4.53, 'V':1.05, 'W':0.011, 'X':0.124, 'Y':1.14,
 *                     'Z':0.324, 'max_val':13.89, 'kappa':0.0766
 *                    }
 * </pre>
 * 
 * @author  Bruce [com.gmail @ ProgrammerBruce]
 * @version 2010.1105
 */
public abstract class LanguageData
{
  public final Map<Character, Double> _frequencies;
  public final double _max_value;
  public final double _kappa;

  LanguageData(Map<Character, Double> frequencies, double max_value, double kappa)
  {
    this._frequencies = frequencies;
    this._max_value = max_value;
    this._kappa = kappa;
  }

  /**
   * Returns a LanguageData object with language specific alphabet character use 
   * statistics, used by VigCrack.
   * <p/>
   * <code>language</code> is a two character String that indicates which 
   * language to use. Possible values for <code>language</code> include:
   * <ul>
   * <li>EN - for English</li>
   * <li>DE - for German</li>
   * <li>ES - for Spanish</li>
   * <li>FR - for French</li>
   * <li>IT - for Italian</li>
   * <li>PT - for Portuguese</li>
   * </ul>
   * 
   * @param language The language for which data is wanted.
   */
  public static LanguageData getData(String language)
  {
    if (language.equals("DE"))
      return new GermanData();
    else if (language.equals("ES"))
      return new SpanishData();
    else if (language.equals("FR"))
      return new FrenchData();
    else if (language.equals("IT"))
      return new ItalianData();
    else if (language.equals("PT"))
      return new PortugueseData();
    else
      return new EnglishData();
  }
}